package com.xyht.sca_s.student_manage_system.modules.news.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.annotation.RepeatRequestCheck;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.news.entity.SmsNews;
import com.xyht.sca_s.student_manage_system.modules.news.entity.dto.SmsNewsAddDto;
import com.xyht.sca_s.student_manage_system.modules.news.entity.dto.SmsNewsIdDto;
import com.xyht.sca_s.student_manage_system.modules.news.entity.dto.SmsNewsUpdateDto;
import com.xyht.sca_s.student_manage_system.modules.news.service.SmsNewsService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2022-12-08
 */
@RestController
@RequestMapping("/news")
@SecretBody
public class SmsNewsController {
    @Resource
    private JwtTokenUtil jwtTokenUtil;
    @Resource
    private SmsNewsService smsNewsService;


    @ApiModelProperty(value = "添加新闻")
    @PostMapping("/teacher/add")
    public ResponseResult addNews(@RequestBody SmsNewsAddDto smsNewsAddDto, HttpServletRequest request){
        String newsId = jwtTokenUtil.getUserIdByRequest(request);
        return smsNewsService.addNews(smsNewsAddDto,newsId);
    }

    @ApiModelProperty(value = "删除新闻")
    @PostMapping("/teacher/delete")
    public ResponseResult deleteNews(@RequestBody SmsNews SmsNews){
        return smsNewsService.deleteNews(SmsNews);
    }

    @ApiModelProperty(value = "修改新闻")
    @PostMapping("/teacher/update")
    public ResponseResult updateNews(@RequestBody SmsNewsUpdateDto smsNewsUpdateDto,HttpServletRequest request){
        String newsId = jwtTokenUtil.getUserIdByRequest(request);
        return smsNewsService.updateNews(smsNewsUpdateDto,newsId);
    }

    @ApiModelProperty(value = "查看一条新闻")
    @GetMapping("/id")
    public ResponseResult getNewsById(HttpServletRequest request,
                                      @RequestParam(value = "id", required = false)String id){
        String viewNewsId = jwtTokenUtil.getUserIdByRequest(request);
        return smsNewsService.getNewsById(id,viewNewsId);
    }

    @ApiModelProperty(value = "后台查看所有新闻")
    @GetMapping("/teacher/all")
    public ResponseResult getAllNews(HttpServletRequest request,
                                     @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "type", required = false)String type,
                                     @RequestParam(value = "search", required = false)String search,
                                     @RequestParam(value = "beginTime", required = false) String beginTime,
                                     @RequestParam(value = "endTime", required = false) String endTime){
        String newsId = jwtTokenUtil.getUserIdByRequest(request);
        return smsNewsService.getAllNews(newsId,pageSize,pageNum,type,search,beginTime,endTime);
    }

    @ApiModelProperty(value = "学生端查看所有新闻")
    @GetMapping("/student/all")
    public ResponseResult getAllStudentNews(HttpServletRequest request,
                                            @RequestParam(value = "newsType", required = false)String newsType,
                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum){
        String newsId = jwtTokenUtil.getUserIdByRequest(request);
        return smsNewsService.getAllStudentNews(newsId,newsType,pageSize,pageNum);
    }

    @ApiModelProperty(value = "点赞功能")
    @PostMapping("/likes")
    @RepeatRequestCheck
    public ResponseResult likes(HttpServletRequest request,@RequestBody SmsNewsIdDto newsId){
        String likeUserId = jwtTokenUtil.getUserIdByRequest(request);
        return smsNewsService.likes(likeUserId,newsId);
    }


    @ApiModelProperty(value = "收藏功能")
    @PostMapping("/collect")
    @RepeatRequestCheck
    public ResponseResult collect(HttpServletRequest request,@RequestBody SmsNewsIdDto newsId){
        String collectUserId = jwtTokenUtil.getUserIdByRequest(request);
        return smsNewsService.collect(collectUserId,newsId);
    }

}

